<template>
<Card style="height:900px">
    <Form ref="form" :model="form" :rules="rules" :label-width="80">
        <FormItem label="机构代码" prop="orgCode">
            <Input v-model="form.orgCode" placeholder="" :disabled="type==1">
            </Input>
        </FormItem>
        <FormItem label="机构名称" prop="orgName">
            <Input v-model="form.orgName" placeholder="">
            </Input>
        </FormItem>
        <FormItem label="上级机构" prop="parentOrgName">
            <Input v-model="form.parentOrgName" disabled placeholder="">
            </Input>
        </FormItem>
        <FormItem label="描述" prop="description">
            <Input v-model="form.description" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="">
            </Input>
        </FormItem>
        <FormItem v-if="type==1 || type ==2 || type==3">
            <Button type="primary" @click="handleSubmit">确定</Button>
            <Button type="" style="margin-left: 8px" @click="handleBack">返回</Button>
        </FormItem>
        <FormItem v-if="type==0">
            <Button type="primary" @click="handleGo(2)">编辑</Button>
            <Button type="error" @click="deleteOrg">删除</Button>
            <Button type="" style="margin-left: 8px" @click="handleBack">返回</Button>
        </FormItem>
    </Form>
</Card>
</template>

<script>
export default {
  data () {
    return {
      baseUrl: this.$appContext.marvels_core_system,
      $ajax: null,
      form: {
        orgCode: null,
        orgName: null,
        parentOrgName: null,
        description: null
      },
      rules: {
        orgCode: [{
          required: true,
          message: '机构代码不能为空',
          trigger: 'blur'
        }],
        orgName: [{
          required: true,
          message: '机构名称不能为空',
          trigger: 'blur'
        }]
      },
      copydata: {}
    }
  },
  computed: {
    type () { // 0修改 1新增子节点 3新增根节点
      return this.$route.query.type
    }
  },
  methods: {
    handleSubmit () {
      var vm = this
      this.$refs.form.validate(valid => {
        if (valid) {
          var config = {
            title: '提示'
          }
          if (this.type === 2) {
            config.content = '是否确认修改?'
            config.onOk = vm.updateOrg
          } else if (this.type === 1) {
            config.content = '是否确认新建?'
            config.onOk = vm.addOrg
          } else if (this.type === 3) {
            config.content = '是否确认新建?'
            config.onOk = vm.addRootOrg
          }
          vm.$Modal.confirm(config)
        }
      })
    },
    updateOrg: function () {
      this.editOrg(this.baseUrl + 'system/pmsn/service/updateOrg')
    },
    addOrg: function () {
      this.editOrg(this.baseUrl + 'system/pmsn/service/addOrg')
    },
    editOrg: function (url) {
      var vm = this
      var org = this.form
      this.$ajax.post(url, org).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code !== 'success') {
          this.$Message.warning(message)
          return
        }
        vm.handleBack()
      }).catch(er => {

      })
    },
    addRootOrg: function () {
      var vm = this
      var org = vm.form
      this.$ajax.post(this.baseUrl + 'system/pmsn/service/addRootOrg', org).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code !== 'success') {
          this.$Message.warning(message)
          return
        }
        this.handleBack()
      }).catch(er => {
      })
    },
    initpage: function () {
      var params = this.$route.query || {}
      if (this.type === 3) { // 新增根节点
        this.form = {}
      } else {
        var vm = this
        var orgId = params.orgId
        var obj = {
          orgId: orgId
        }
        this.$ajax.post(this.baseUrl + 'system/pmsn/service/getOrgInfo', obj).then(re => {
          var data = re.data
          var code = data.code
          var message = data.message
          if (code !== 'success') {
            this.$Message.warning(message)
            return
          }
          if (vm.type === 1) { // 新增子节点
            vm.form = {
              parentOrgName: data.data.orgName,
              parentOrgCode: data.data.orgCode
            }
          } else { // 查看  0修改
            vm.form = data.data
          }
          vm.copydata = JSON.parse(JSON.stringify(data.data))
        }).catch(er => {

        })
      }
    },
    handleBack: function () {
      this.$router.back()
    }
  },
  mounted () {
    this.$ajax = this.$util.ajax()
    this.initpage()
  },
  created: function () {

  }
}
</script>
